//
//  AddPersonViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/21.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "AddPersonViewController.h"
#import "TAMacro.h"
#import "AFNetworking.h"
#import "SearchPersonModel.h"
#import "SearchPersonContent.h"
#import "SearchPersonTableViewCell.h"
@interface AddPersonViewController ()
<
    UISearchBarDelegate,
    UITableViewDelegate,
    UITableViewDataSource
>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, copy) NSString *searchText;
@property (nonatomic, strong) UITableView *searchTableView;
@property (nonatomic, strong) SearchPersonModel *searchModel;

@end

@implementation AddPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = 1;
    _titleLabel.text = @"帖子详情";
    [self.backView addSubview:_titleLabel];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

    
//    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
//    searchBarView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
//    [self.view addSubview:searchBarView];
    
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH - 50, 50)];
    _searchBar.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    [_searchBar setBarTintColor:[UIColor colorWithWhite:0.917 alpha:1]];
    _searchBar.backgroundImage = [UIImage new];
    self.searchBar.layer.cornerRadius = 4;
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.delegate = self;
    [self.searchBar setPlaceholder:@"搜索用户昵称"];
    [self.view addSubview:_searchBar];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.frame = CGRectMake(CGRectGetMaxX(_searchBar.frame), 64, 50, 50);
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelButton setTitleColor:[UIColor colorWithRed:0.002 green:0.967 blue:0.580 alpha:1.000] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
    
    
    
    [self createView];
}

- (void)backAction:(UIButton *)backButton {
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)netWork {
    // c=SysFunc&m=search&p=%7B%22key%22%3A%22%E5%93%88%22%7D&t=t&v=v105
    NSString *text = [NSString stringWithFormat:@"{\"key\":\"%@\"}", _searchText];
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
       NSString *parmeters = [NSString stringWithFormat:@"c=SysFunc&m=search&p=%@&t=t&v=v105", utf8Text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
        self.searchModel = [SearchPersonModel modelWithDic:responseObject];
        [self.searchTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchModel.content.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchPersonTableViewCellReuse"];
    cell.searchPerson = _searchModel.content[indexPath.row];
    cell.searchKey = _searchText;
    return cell;
}

// 取出在搜索框中输入的字符串
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.searchText = searchText;
}

- (void)createView {
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 114, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStylePlain];
    [self.view addSubview:_searchTableView];
    _searchTableView.delegate = self;
    _searchTableView.dataSource = self;
    [_searchTableView registerNib:[UINib nibWithNibName:@"SearchPersonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SearchPersonTableViewCellReuse"];
}

// 搜索点击方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self netWork];
    [searchBar resignFirstResponder];
}

// 回收键盘的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.searchBar resignFirstResponder];
}

// 滑动tableView键盘回收
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}


- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"点击啥了");
}



- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    NSLog(@"编辑完成");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
